package com.gz.purchase.library.listener;

import com.gz.purchase.library.pojo.UserInfo;
import com.gz.purchase.library.util.Constants;
import com.gz.purchase.library.util.CrashHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * 用户数据监听类
 *
 *
 */
public abstract class LoginListener extends BaseListener {


	private static final String TAG = LoginListener.class.getSimpleName();

	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		
			try {
				int status = response.getInt(Constants.REQ_STATUS);
				String msg = response.getString(Constants.REQ_MSG);
				CrashHandler.i(TAG, "登录服务器status = " + status);
				CrashHandler.i(TAG, "登录服务器msg = " + msg);

				switch(status){
					case Constants.REQ_OK:
						JSONObject jsonObject = response.getJSONObject(Constants.REQ_RESULT);
                        onResponseSuccess(UserInfo.parse(jsonObject));
						break;

					case Constants.REQ_ERROR_USER_NAME:
					case Constants.REQ_ERROR_PWD:
						userNameOrPwdError();
						break;

					case Constants.REQ_USER_FORBIDDENED:
						userForbidden();
						break;

					default:
						onResponseFailed();
				}
				
			} catch (JSONException e) {
				onFailure(statusCode, headers, e, new JSONObject());
			}
	}

	@Override
	public void onFailure(int statusCode, Header[] headers,
			Throwable throwable, JSONObject errorResponse) {
		CrashHandler.e(throwable);
		onResponseFailed();
	}
	
	@Override
	public void onNoMoreData() {

	}
	
	@Override
	public void onOffline() {

	}

	/**
	 * 加载用户信息成功
	 * @param headers 包含cookie信息的http头
	 * @param userInfo 账户信息; 如果不存在账户的情况下, 将返回null
	 */
	public void onResponseSuccess(Header[] headers, UserInfo userInfo) {
		
	}
	
	/**
	 * 加载用户信息成功
	 * @param userInfo 账户信息; 如果不存在账户的情况下, 将返回null
	 */
	public abstract void onResponseSuccess(UserInfo userInfo);

	/**
	 * 用户名或密码错误
	 */
	public abstract void userNameOrPwdError();

	/**
	 * 用户名被禁止使用
	 */
	public abstract void userForbidden();

}
